<?php

namespace Modules\Shop\Entities;

use houdunwang\arr\Arr;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
//    protected $fillable = ['cname','pid'];
    protected $guarded = [];
    public function category(){
        return $this->belongsTo(Category::class,'pid','id');
    }
    public static function category_cate(){
        $data = self::get()->toArray();
        return Arr::tree($data, 'cname','id','pid');
    }
    public static function getTreeCategory ($cate=null){
        $data = self::get();
//        dd($data);
        if(!is_null($cate)){
            foreach ($data as $k=>$v){
                $data[$k]['_selected'] = $cate['pid'] == $v['id'];
                $data[$k]['_disabled'] = $cate['id'] == $v['id']||Arr::isChild($data, $v['id'], $cate['id'], $fieldPri = 'id', $fieldPid = 'pid');
            }
        }
//        dd($data->toarray());
        $category = Arr::tree($data ? $data->toArray():[], 'cname', $fieldPri = 'id', $fieldPid = 'pid');
//        dd($category);
        return $category;
    }
    public static function hasChildCategory($cate){
        $data = self::get();
        return Arr::hasChild($data, $cate['id'], $fieldPid = 'pid');
    }

}
